## R Script Output -------------------------------------------------------------
# Figure 1: Immigration Lobbying by U.S. Firms, 1999--2017


## Instructions ----------------------------------------------------------------
# Step 1: Adjust MAIN_DIR to where README.txt is located
# Step 2: Run entire script


## setup -----------------------------------------------------------------------
# clean slate
rm(list = ls())
date()

# load packages
pkg <- c("tidyverse",
         "RColorBrewer", 
         "gridExtra", 
         "viridis")

lapply(pkg, require, character.only = TRUE)

# set main directory
MAIN_DIR <- "~/Dropbox/Research/JOP-h1b-replication"


## load data -------------------------------------------------------------------
load(file = paste(MAIN_DIR, "lv-imm-fr-99-17-rep.RData", sep = "/"))


## Figure 1: left panel --------------------------------------------------------
expense.df <- lob.img.reports.replication %>%
  group_by(year) %>%
  summarize(tot_expense = sum(mean_img_expense_report, na.rm = TRUE)) %>%
  ungroup()

# set parameters
axis.title.size <- 16

# plot
p.expense.trend <- ggplot(expense.df, 
                          aes(x = year, y = tot_expense / 1000000)) +
  geom_rect(xmin = 2016.5, xmax = Inf, 
            ymin = -Inf, ymax = Inf, 
            fill = "grey90", color = "grey90",
            alpha = 0.1) +
  geom_rect(xmin = 2000.5, xmax = 2003.5, 
            ymin = -Inf, ymax = Inf, 
            fill = "grey90", color = "grey90",
            alpha = 0.1) +
  geom_rect(xmin = 2007.5, xmax = 2010.5, 
            ymin = -Inf, ymax = Inf, 
            fill = "grey90", color = "grey90",
            alpha = 0.1) +
  geom_point(size = 3) +
  geom_line() +
  scale_y_continuous("Estimated IMM Lobbying Expenses (millions)") +
  scale_x_continuous("Year",
                     breaks = seq(1999, 2017, by = 2),
                     labels = seq(1999, 2017, by = 2),
                     limits = c(1999, 2019.5)) +
  ggtitle("Firm-Lobbying on Immigration") +
  theme_bw() + 
  theme(plot.title = element_text(size = axis.title.size,
                                  face = "bold",
                                  margin = margin(0, 0, 20, 0),
                                  hjust = 0.5),
        axis.title.y = element_text(size = axis.title.size + 1,
                                    margin = margin(0, 5, 0, 0)),
        axis.title.x = element_text(size = axis.title.size + 1,
                                    margin = margin(20, 0, 0, 0)),
        axis.text = element_text(size = axis.title.size - 3),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        legend.position = "none")

# add annotation
text.size <- axis.title.size - 10

p.expense.trend <- p.expense.trend + 
  annotate("text", 
           x = 2017, 
           y = 38.5,
           label = "Trump\nAdmin.", 
           color = "black",
           hjust = 0,
           size = text.size - 2) + 
  annotate("segment", 
           x = 2017, xend = 2019, 
           y = 41, yend = 41, 
           colour = "black",
           arrow = arrow(length = unit(0.2,"cm"), 
                         ends = "last", type = "closed")) +
  annotate("text", 
           x = 2002, 
           y = 38.2,
           label = "H-1B\nCap\nRelaxed", 
           color = "black",
           hjust = 0.5,
           size = text.size - 1.5) + 
  annotate("text", 
           x = 2009, 
           y = 38.2,
           label = "Global\nFinancial\nCrisis", 
           color = "black",
           hjust = 0.5,
           size = text.size - 1.5)

# save
pdf(paste(MAIN_DIR, "Figure-1-left.pdf", sep = "/"), 
    width = 6, height = 6)
p.expense.trend
dev.off()


## Figure 1: right panel -------------------------------------------------------
# summarize report venue data and create variables of interest
venues.df <- lob.img.reports.replication %>%
  group_by(year) %>%
  mutate(n_img_rep_year = n_distinct(report_id)) %>%
  summarize(n_img_rep_year = first(n_img_rep_year),
            n_dhs = sum(lob_dhs, na.rm =  TRUE),
            n_dol = sum(lob_dol, na.rm =  TRUE),
            n_house = sum(lob_house, na.rm =  TRUE),
            n_senate = sum(lob_senate, na.rm =  TRUE),
            n_wh = sum(lob_wh, na.rm =  TRUE),
            n_eop = sum(lob_eop, na.rm =  TRUE)) %>%
  ungroup() %>%
  pivot_longer(cols = n_dhs:n_eop,
               names_to = "venue",
               names_prefix = "n_",
               values_to = "n_reports") %>%
  mutate(share_reports = n_reports / n_img_rep_year)

# set gov. entities
visa.agency.vec <- c("house", 
                     "senate", 
                     "dhs", 
                     "dol", 
                     "wh", 
                     "eop")

# subset
venues.df.sub <- venues.df %>%
  filter(venue %in% visa.agency.vec) %>%
  mutate(venue = factor(venue, levels = visa.agency.vec))

# set parameters
axis.title.size <- 16

colors <- c("#377EB8", # House
            "#E41A1C", # Senate
            "#E6AB02", # DHS
            "#1B9E77", # DOL
            "black", # White House
            "#D95F02" # EOP
            )

shapes <- c(2, # House
            1, # Senate
            16, # DHS
            8, # DOL
            15, # WH
            17 # EOP
            )

# plot
p.top.venue.trend <- ggplot(venues.df.sub, 
                            aes(x = year, y = share_reports, 
                                group = factor(venue),
                                color = factor(venue),
                                shape = factor(venue))) +
  geom_rect(xmin = 2016.5, xmax = Inf, 
            ymin = -Inf, ymax = Inf, 
            fill = "grey90", 
            color = "grey90",
            alpha = 0.1) +
  geom_point(size = 3) +
  geom_line() +
  scale_color_manual(values = colors) +
  scale_shape_manual(values = shapes) +
  scale_y_continuous("Share of Immigration Lobbying Reports",
                     breaks = seq(0, 1, by = 0.25),
                     limits = c(-0.02, 1.12)) +
  scale_x_continuous("Year",
                     breaks = seq(1999, 2017, by = 2),
                     labels = seq(1999, 2017, by = 2),
                     limits = c(1999, 2019.5)) +
  ggtitle("Top IMM Lobbying Venues in 2017") +
  theme_bw() + 
  theme(plot.title = element_text(size = axis.title.size,
                                  face = "bold",
                                  margin = margin(0, 0, 20, 0),
                                  hjust = 0.5),
        axis.title.y = element_text(size = axis.title.size + 1,
                                    margin = margin(0, 5, 0, 0)),
        axis.title.x = element_text(size = axis.title.size + 1,
                                    margin = margin(20, 0, 0, 0)),
        axis.text = element_text(size = axis.title.size - 3),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        legend.position = "none")

# add annotation
x.pos <- 2017.4
text.size <- axis.title.size - 11

# House
p.top.venue.trend <- p.top.venue.trend + 
  annotate("text", 
           x = x.pos, 
           y = venues.df.sub %>%
             filter(year == 2017 & 
                      venue == "house") %>%
             pull(share_reports),
           label = "House", 
           color = colors[[1]],
           hjust = 0,
           size = text.size)

# Senate
p.top.venue.trend <- p.top.venue.trend + 
  annotate("text", 
           x = x.pos, 
           y = venues.df.sub %>%
             filter(year == 2017 & 
                      venue == "senate") %>%
             pull(share_reports),
           label = "Senate", 
           color = colors[[2]],
           hjust = 0,
           size = text.size)

# DHS
p.top.venue.trend <- p.top.venue.trend + 
  annotate("text", 
           x = x.pos, 
           y = venues.df.sub %>%
             filter(year == 2017 & 
                      venue == "dhs") %>%
             pull(share_reports),
           label = "DHS", 
           color = colors[[3]],
           hjust = 0,
           size = text.size)

# DOL
p.top.venue.trend <- p.top.venue.trend + 
  annotate("text", 
           x = 2014, 
           y = -0.02,
           label = "DOL", 
           color = colors[[4]],
           hjust = 0,
           size = text.size)

# WH
p.top.venue.trend <- p.top.venue.trend + 
  annotate("text", 
           x = x.pos, 
           y = venues.df.sub %>%
             filter(year == 2017 & 
                      venue == "wh") %>%
             pull(share_reports),
           label = "WH", 
           color = colors[[5]],
           hjust = 0,
           size = text.size)

# EOP
p.top.venue.trend <- p.top.venue.trend + 
  annotate("text", 
           x = x.pos, 
           y = venues.df.sub %>%
             filter(year == 2017 & 
                      venue == "eop") %>%
             pull(share_reports) - 0.02,
           label = "EOP", 
           color = colors[[6]],
           hjust = 0,
           size = text.size)

# add administration
p.top.venue.trend <- p.top.venue.trend + 
  annotate("text", 
           x = 2017, 
           y = 1.05,
           label = "Trump\nAdmin.", 
           color = "black",
           hjust = 0,
           size = text.size - 1) + 
  annotate("segment", 
           x = 2016.9, xend = 2019, 
           y = 1.12, yend = 1.12, 
           colour = "black",
           arrow = arrow(length = unit(0.2,"cm"), 
                         ends = "last", type = "closed"))

# save
pdf(paste(MAIN_DIR, "Figure-1-right.pdf", sep = "/"), 
    width = 6, height = 6)
p.top.venue.trend
dev.off()

